﻿using System.Collections.Generic;
using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using TestApp.Model;
using TestApp.Model.Managers;
using TestApp.Navigation;
using TestApp.Messages;
using TestApp.WebService;

namespace TestApp.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class CouponsListViewModel : ViewModelBase
    {

        // Property Names
        private const string WelcomeTitlePropertyName = "WelcomeTitle";
        private const string CouponsListPropertyName = "CouponsList";
        private const string ListPickerIsEnabledProperty = "ListPickerIsEnabled";
        private const string CategoriesPropertyName = "Categories";
        private const string SelectedIndexProperty = "SelectedIndex";


        // Navigation Service
        private INavigationService NavigationService;

        // Message Sender
        private MessageSender<CouponInitMessage> CouponSender;


        // Private attirbutes for the "Properties"
        private List<CouponDetailsViewModel> _coupons = null;
        private bool _listPickerIsEnabled = false;
        private List<CouponCategory> _categories;

        /// <summary>
        /// Initializes a new instance of the CouponsListViewModel class.
        /// </summary>

        public CouponsListViewModel(INavigationService navigationService)
        {
            OnPageLoadedCommand = new RelayCommand(PageLoaded);
            OnSelectionChanged = new RelayCommand<Int32>((c) => SelectionChanged(c));
            NavigationService = navigationService;

        }
       
        // Page Loaded Command
        public RelayCommand OnPageLoadedCommand
        {
            get;
            private set;
        }

        // Selection Changed Command
        public RelayCommand<Int32> OnSelectionChanged
        {
            get;
            private set;
        }

        // Selected Item
        public Int32 SelectedIndex
        {
            get
            {
                return -1;
            }
        }

       // CouponsListProperty
        public List<CouponDetailsViewModel> CouponsList
        {
            get
            {
                return _coupons;
            }

            set
            {
                _coupons = value;
                RaisePropertyChanged(CouponsListPropertyName);
            }
        }

        // Categories Button Text
        public bool ListPickerIsEnabled
        {
            set
            {
                _listPickerIsEnabled = value;
                RaisePropertyChanged(ListPickerIsEnabledProperty);
            }
            get
            {
                return _listPickerIsEnabled;
            }
        }

        // Categories
        public List<CouponCategory> Categories
        {
            get
            {
                return _categories;
            }
            set
            {
                _categories = value;
                RaisePropertyChanged(CategoriesPropertyName);
            }
        }

        // Page Loaded
        private async void PageLoaded()
        {
            if (_coupons == null)
            {
                try
                {
                    List<Coupon> coupons = await CouponsManager.Instance.FetchCoupons();
                    _coupons = new List<CouponDetailsViewModel>();
                    foreach (Coupon coupon in coupons)
                    {
                        _coupons.Add(new CouponDetailsViewModel(coupon));
                    }
                    CouponsList = _coupons;
                    Categories = CouponsManager.Instance.CategoriesForCoupons(coupons);
                    ListPickerIsEnabled = true;
                }

                catch (WSException exception)
                {
                    // Web Service Down or Connection Error
                    Console.WriteLine(exception.Message);
                }
            }
            else
            {
                RaisePropertyChanged(CouponsListPropertyName);
            }
        }

        // Selection Changed
        private void SelectionChanged(Int32 selectedIndex)
        {
            if (selectedIndex != -1) {
                RaisePropertyChanged(SelectedIndexProperty);
                if (CouponSender == null)
                {
                    CouponSender = new MessageSender<CouponInitMessage>();
                } 
                CouponSender.SendMessage(new CouponInitMessage (CouponsList[selectedIndex].CurrentCoupon));
                NavigationService.NavigateTo(new Uri(ViewModelLocator.CouponsDetailsPage, UriKind.Relative));
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}